package controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/3/21 11:39
 * @description：我的第三个servlet类
 * @modified By：
 * @version: 1.0
 */
public class UserServlet extends HttpServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
       // req.setCharacterEncoding("UTF-8");  此处属于硬编码，应该放到配置文件中
        //获取上下文
        ServletContext servletContext = getServletContext();
        //获取全局配置
        String encode = servletContext.getInitParameter("encode");
        //设置编码格式
        req.setCharacterEncoding(encode);
        res.setContentType("text/html;charset=utf-8");
        res.setCharacterEncoding(encode);
        System.out.println(encode);
        String userKey = getInitParameter("userKey");
        System.out.println("UserServlet中获取局部参数："+userKey);


        String name = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("用户名"+name);
        System.out.println("密码"+password);

        System.out.println("打印当前当前servlet的名字："+getServletName());
        System.out.println("获取所有的局部参数名");
        Enumeration<String> parameterNames = getInitParameterNames();
        while (parameterNames.hasMoreElements()){
            System.out.println( parameterNames.nextElement());
        }
        //获取所有参数
        String[] values = req.getParameterValues("hobby");
        for (String value : values) {
            System.out.println(value);
        }
        Object attribute = req.getAttribute("username");
        System.out.println("username属性值"+attribute);
        res.getWriter().print("<h1>欢迎："+name+"</h1>");
    }
}
